<template>
    <div id='' class='sys_manage_form_content' :style="{'--height':hight?hight+'px':'670px'}">
        <i-form ref="typeForm" :config="configHeader" style="margin-top:-40px;">
            <template #myslot="rowData">
                <RadioGroup v-model="type" @on-change="radioOn">
                    <Radio label="0">菜单</Radio>
                    <Radio label="1">按钮</Radio>
                </RadioGroup>
            </template>
            <template #systemType="rowData">
                <RadioGroup v-model="systemType">
                    <Radio label="0">后台</Radio>
                    <Radio label="1">前台</Radio>
                </RadioGroup>
            </template>
        </i-form>
        <MenuForm v-if="type=='0'" ref="menuForm" :isInit="isInit" />
        <BtnForm v-if="type=='1'" ref="buttonForm" :isInit="isInit" />
    </div>
</template>
<script>
import BtnForm from './button_form.vue'
import MenuForm from './menu_form.vue'
import { configHeader } from './form_config'

import { mapActions, mapGetters } from 'vuex'
export default {
    name: '',
    inject: ['provideClick'],
    props: ['hight', 'isInit'],
    data() {
        this.ch = icu.util.deepClone(configHeader)
        return {
            configHeader: this.ch,
            type: '0',
            systemType: '1',
            rowInfo: {},
            // '$row': null,// 默认赋值
        };
    },
    components: { BtnForm, MenuForm },
    computed: {},
    beforeMount() { },
    mounted() {
        this.rowInfo = {}
    },
    methods: {
        ...mapActions('extend/sysMenu', ['setData']),
        ...mapGetters('extend/sysMenu', ['getData']),
        reset() {
            if (this.type == '0') {
                this.$refs.menuForm.reset()
            } else {
                this.$refs.buttonForm.reset()
            }
        },
        setUpMenu() {
            // this.$row = thisRow
            if (this.type == '0') {
                this.$nextTick(() => {
                    this.$refs.menuForm.setUpMenu()
                })
            } else {
                this.$nextTick(() => {
                    this.$refs.buttonForm.setUpMenu()
                })
            }
        },
        getFormData(cb) {
            let menuFormData = {};
            if (this.type == '0') {
                menuFormData = this.setForm(this.$refs.menuForm.getForm())
            } else {
                menuFormData = this.setForm(this.$refs.buttonForm.getForm())
            }
            this.$http.$post('menuSave', { data: menuFormData }).then(res => {
                if (res.code == 200) {
                    this.$Message.success('保存成功');
                    if (cb) {
                        res.data['contextmenu'] = true
                        this.setData([{
                            key: 'respondData',
                            value: res.data
                        }, {
                            key: '$row',
                            value: null
                        }])
                        cb(res)
                    }
                }
            })
        },
        radioOn() {
            this.setInit(this.rowInfo, this.type)
            // 切换按钮默认上级菜单和所属系统赋值
            if (this.getData()('$row')) {
                this.setUpMenu()
            }
        },
        setInit(row, type) {
            this.rowInfo = row
            this.$http.$get('detailMenu', { params: { id: row.id } }).then(res => {
                if (res.code == 200) {
                    if (!res.data) {
                        return this.provideClick('default', {})
                    }
                    let resData = res.data
                    this.type = type ? type : resData.type.toString();
                    this.systemType = resData.systemType.toString();
                    if (this.type == '0') {
                        // 初始化菜单
                        this.$nextTick(() => {
                            this.reset()
                            this.$refs.menuForm.init(resData)
                        })
                    } else {
                        // 初始化按钮
                        this.$nextTick(() => {
                            this.reset()
                            this.$refs.buttonForm.init(resData)
                        })
                    }
                }
            })
        },
        setForm(data) {
            return {
                backgroundDocId: data.backgroundDocId ? data.backgroundDocId : '',
                floorIcon: data.floorIcon ? data.floorIcon : '',
                floorRemarks: data.floorRemarks ? data.floorRemarks : '',
                hideSider: data.hideSider ? data.hideSider : 0,
                isCached: data.isCached,
                icon: data.icon ? data.icon : '',
                id: this.rowInfo.id ? this.rowInfo.id : data.id ? data.id : '',
                name: data.name ? data.name : '',
                permissions: data.permissions ? data.permissions : '',
                pid: data.pid ? data.pid : '',
                remarks: data.remarks ? data.remarks : '',
                route: data.route ? data.route : '',
                sort: data.sort ? data.sort * 1 : 0,
                subsystemId: data.subsystemId ? data.subsystemId : '',
                systemType: this.systemType,
                type: this.type,
            }
            //   let sendObj = { backgroundDocId: '', floorIcon: '', floorRemarks: '', hideSider: '', icon: '', id: '', name: '', permissions: '', pid: '', remarks: '', route: '', sort: '', subsystemIdZ: '', systemType: this.systemType, type: this.type, }
        }
    },
    watch: {}
}
</script>
<style lang='less' scoped>
.sys_manage_form_content {
    height: var(--height);
    overflow: auto;
}
</style>